// const sequelize = require('@/db/seq');
const { DataTypes } = require('sequelize');
const sequelize = require("@/db/seq");

const DictionaryType = sequelize.define('dictionary_type', {
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '名称'
  },
  code: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
    comment: '编码'
  },
  description: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '描述'
  }
}, {
  freezeTableName: true
})
// DictionaryType.sync({
//   // force: false, // 是否强制同步，true: 强制同步，false: 不强制同步
//   alter: true, // 是否自动更新表结构，true: 自动更新，false: 不自动更新
// })
// .then(() => {
//   console.log('dictionary_ctype table synced successfully');
// })
// .catch((err) => {
//   console.error('Error syncing dictionary_type  table:', err);
// })

DictionaryType.associate = (models) => {
  DictionaryType.hasMany(models.dictionary_items, {
    foreignKey: 'type_id',
    as: 'itemsById'
  });
  DictionaryType.hasMany(models.dictionary_items, {
    foreignKey: 'type_code',
    sourceKey: 'code',
    as: 'itemsByCode'
  });
};
module.exports = DictionaryType
